//
//  TabItem.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/5/31.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "TabItem.h"

@interface TabItem ()

@property (nonatomic, strong) UIImageView *hotImageView;
@end

#define TabBarButtonImageRatio 0.60
@implementation TabItem

+ (TabItem *)initTabItemWith:(NSDictionary *)dict {
    TabItem * item = [[TabItem alloc] init];
    
    [item setImage:[UIImage imageNamed:dict[@"imageName"]] forState:UIControlStateNormal];
    [item setTitle:MYLocalizedString(dict[@"title"], nil) forState:UIControlStateNormal];
    [item setTitleColor:kSubitleColor forState:UIControlStateNormal];
    item.titleLabel.font = [UIFont systemFontOfSize:12];
    item.titleLabel.textAlignment = NSTextAlignmentCenter;
    item.titleLabel.numberOfLines = 0;
    return item;
}


- (float)Ratio {
    
    if (_Ratio == 0) {
        _Ratio = TabBarButtonImageRatio;
    }
    
    return _Ratio;
}
- (void)setIsHot:(BOOL)isHot {
    _isHot = isHot;
    if (isHot) {
        [self showHotImageView];
    }
}

- (void)showHotImageView {
    
    if (!_hotImageView) {
        _hotImageView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"hot"]];
        [self addSubview:_hotImageView];
        
        @weakify(self);
        [_hotImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            @strongify(self);
            make.top.equalTo(self.imageView.mas_top);
            make.right.equalTo(self.imageView.mas_right);
            make.width.equalTo(self.imageView.mas_width).multipliedBy(2/ 5.0f);
            make.height.equalTo(self.imageView.mas_width).multipliedBy(2/ 5.0f);
        }];
    }
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    CGFloat imageW = contentRect.size.width - 10;
    CGFloat imageH = imageW;
    
    return CGRectMake(5, 0, imageW, imageH);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    
     CGFloat imageH = contentRect.size.width - 10;
    
    CGFloat titleY = imageH - 5;
    CGFloat titleW = contentRect.size.width + 10;
    CGFloat titleH = 40;
    
    return CGRectMake(-5, titleY, titleW,titleH);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
